package com.oocl.homework.crm;


import java.util.Scanner;

import com.oocl.homework.crm.command.AddCommand;
import com.oocl.homework.crm.command.Command;
import com.oocl.homework.crm.command.CommandDoc;
import com.oocl.homework.crm.command.DeleteCommand;
import com.oocl.homework.crm.command.LookCommand;
import com.oocl.homework.crm.command.OrderCommand;
import com.oocl.homework.crm.command.UpdateCommand;
import com.oocl.homework.crm.command.HelpCommand;
import com.oocl.homework.crm.command.QuitCommand;
import com.oocl.homework.crm.datastruct.UserDaoImpl;
import com.oocl.homework.crm.model.User;
import com.oocl.homework.crm.model.UserFilter;

public class CommandExecutor{
	
	
	private UserDaoImpl userDaoImpl;
	
	private UserFilter userFilter;
	
	public CommandExecutor(){
		userDaoImpl = UserDaoImpl.getInstance();
		userFilter = new UserFilter();
		
	}
	public void start(){
		System.out.println("================================= Welcome ======================= ");
    	CommandDoc.printHelp();
		Scanner scanner = new Scanner(System.in);
    	
        while (scanner.hasNextLine()) {
			String commandString = scanner.nextLine();
			if(commandString!=null&&!commandString.equals("")){
				String ch = commandString.split(" ")[0];
			
				switch (ch) {
				case "A":
					addCommand(commandString);
					break;
				case "D":
					deleteCommand(commandString);
					break;
				case "L":
					lookCommand(commandString);
					break;
				case "U":
					updateCommand(commandString);
					break;
				case "O":
					orderCommand(commandString);
					break;
				case "H":
					helpCommand(commandString);
					break;
				case "Q":
					QuitCommand(commandString);
					break;
				default:
					System.out.println("Unrecognized Command:"+ch);
					break;
				}
			}
			
			
			
			
		}
	}
	
	
	
	
	private void addCommand(String commandString){
		 Command command = new AddCommand<User>(commandString);
		 User user = new User();
		 command.filter(userFilter);
		 if(command.getResult().getCode()==-1){
			 command.display();
			 return;
		 }
		 user.setAttributes(command.getCommandParameters());
		
	     command.execute(userDaoImpl,user);
	}
	
	
	private void deleteCommand(String commandString){
		 Command command = new DeleteCommand<User>(commandString);
		 User user = new User();
		 command.filter(userFilter);
		 if(command.getResult().getCode()==-1){
			 command.display();
			 return;
		 }
		 user.setId(Integer.valueOf(command.getCommandData()));;
	     command.execute(userDaoImpl,user);
	}
	
	private void updateCommand(String commandString){
		 Command command = new UpdateCommand<User>(commandString);
		 User user = new User();
		 command.filter(userFilter);
		 if(command.getResult().getCode()==-1){
			 command.display();
			 return;
		 }
		 user.setAttributes(command.getCommandParameters());
		 user.setId(Integer.valueOf(command.getCommandData()));;
	     command.execute(userDaoImpl,user);
	}
	
	private void lookCommand(String commandString){
		 Command command = new LookCommand<User>(commandString);
		 User user = new User();
		 command.filter(userFilter);
		 if(command.getResult().getCode()==-1){
			 command.display();
			 return;
		 }
		 if (command.getCommandParameters()==null) {
			 command.execute(userDaoImpl,null);
			 return;
		}
		 user.setAttributes(command.getCommandParameters());
	     command.execute(userDaoImpl,user);
	}
	
	private void orderCommand(String commandString){
		 Command command = new OrderCommand<User>(commandString);
		 command.filter(null);
		 if(command.getResult().getCode()==-1){
			 command.display();
			 return;
		 }
	     command.execute(userDaoImpl,null);
	}
	
	private void helpCommand(String commandString){
		Command command = new HelpCommand<>(commandString);
		command.filter(null);
		 if(command.getResult().getCode()==-1){
			 command.display();
			 return;
		 }
	     command.execute(null,null);
	}
	
	private void QuitCommand(String commandString){
		Command command = new QuitCommand <>(commandString);
		command.filter(null);
		 if(command.getResult().getCode()==-1){
			 command.display();
			 return;
		 }
	     command.execute(null,null);
	}
	
	private void execute(Command command){
		 User user = new User();
		 command.filter(userFilter);
		 if(command.getResult().getCode()==-1){
			 command.display();
			 return;
		 }
		 user.setAttributes(command.getCommandParameters());
	     command.execute(userDaoImpl,user);
	}
}
